

//
//  ShoppingCartTableViewCell.m
//  ChineseCollegeStudentsNetwork
//
//  Created by 鹿达令 on 2019/3/23.
//  Copyright © 2019年 刘喆. All rights reserved.
//

#import "ShoppingCartTableViewCell.h"

@implementation ShoppingCartTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self.contentView addSubview:self.checkButton];
        [self.contentView addSubview:self.bookImageView];
        [self.contentView addSubview:self.titleLabel];
        [self.contentView addSubview:self.priceLabel];
//        [self.contentView addSubview:self.shopingCartView];
        [self.contentView addSubview:self.jianButton];
        [self.contentView addSubview:self.numberTextField];
        [self.contentView addSubview:self.jiaButton];
        
    }
    return self;
}

- (void)setModel:(ShopingCartModel *)model {
    _model = model;
    _titleLabel.text = _model.productName;
    _priceLabel.text = [NSString stringWithFormat:@"¥%@",_model.fee];
    [_bookImageView sd_setImageWithURL:[NSURL URLWithString:_model.imageUrl]];
    _numberTextField.text = _model.count;
    self.checkButton.selected = _model.isSelected;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    [_checkButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@30);
        make.width.equalTo(@30);
        make.left.equalTo(self.contentView.mas_left).offset(25);
        make.centerY.equalTo(self.contentView.mas_centerY);
        
    }];
    [_bookImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.checkButton.mas_right).offset(15);
        make.height.equalTo(@80);
        make.width.equalTo(@60);
        make.centerY.equalTo(self.contentView.mas_centerY);
    }];
    
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bookImageView.mas_right).offset(20);
        make.height.equalTo(@40);
        make.right.equalTo(self.contentView.mas_right).offset(-20);
        make.top.equalTo(self.contentView.mas_top).offset(20);
    }];
    
//    [_shopingCartView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.bottom.equalTo(self.contentView.mas_bottom).offset(-20);
//        make.right.equalTo(self.contentView.mas_right).offset(-20);
//        make.height.equalTo(@20);
//        make.width.equalTo(@60);
//    }];
    
 
    
    [_jiaButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView.mas_right).offset(-20);
        make.height.equalTo(@20);
        make.width.equalTo(@20);
        make.bottom.equalTo(self.contentView.mas_bottom).offset(-20);
    }];
    
    [_numberTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@30);
        make.right.equalTo(self.jiaButton.mas_left);
        make.height.equalTo(@20);
       make.bottom.equalTo(self.contentView.mas_bottom).offset(-20);
    }];
    [_jianButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.numberTextField.mas_left);
        make.height.equalTo(@20);
        make.width.equalTo(@20);
        make.bottom.equalTo(self.contentView.mas_bottom).offset(-20);
    }];

    [_priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.contentView.mas_bottom).offset(-20);
        make.left.equalTo(self.bookImageView.mas_right).offset(20);
        make.height.equalTo(@20);
        make.right.equalTo(self.jianButton.mas_left).offset(-20);
    }];
}

- (UIButton *)checkButton {
    if (!_checkButton) {
        _checkButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_checkButton setImage:[UIImage imageNamed:@"选择2"] forState:UIControlStateNormal];
         [_checkButton setImage:[UIImage imageNamed:@"选中2"] forState:UIControlStateSelected];
        [_checkButton addTarget:self action:@selector(choiceClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _checkButton;
}

- (UIImageView *)bookImageView {
    if (!_bookImageView) {
        _bookImageView = [[UIImageView alloc]init];
        _bookImageView.backgroundColor = [UIColor orangeColor];
    }
    return _bookImageView;
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc]init];
        _titleLabel.textColor = [UIColor blackColor];
        _titleLabel.font = [UIFont systemFontOfSize:14];
        _titleLabel.numberOfLines = 2;
        [_titleLabel sizeToFit];
        
    }
    return _titleLabel;
}

-(UILabel *)priceLabel {
    if (!_priceLabel) {
        _priceLabel = [[UILabel alloc]init];
        _priceLabel.textColor = [UIColor orangeColor];
        _priceLabel.font = [UIFont systemFontOfSize:13];
        _priceLabel.text = @"¥16.0";
        
    }
    return _priceLabel;
}

//- (ShopingCartView *)shopingCartView {
//    if (!_shopingCartView) {
//        _shopingCartView = [[ShopingCartView alloc]initWithFrame:CGRectZero];
//        _shopingCartView.layer.borderWidth = .5f;
//        _shopingCartView.layer.borderColor = [UIColor lightGrayColor].CGColor;
//    }
//    return _shopingCartView;
//}

- (UIButton *)jianButton {
    if (!_jianButton) {
        _jianButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _jianButton.layer.borderColor = [UIColor lightGrayColor].CGColor;
        _jianButton.layer.borderWidth = .5f;
        [_jianButton setTitle:@"-" forState:UIControlStateNormal];
        [_jianButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        [_jianButton addTarget:self action:@selector(delClick:) forControlEvents:UIControlEventTouchUpInside];
        _jianButton.titleLabel.font = [UIFont systemFontOfSize:13];
        
    }
    return _jianButton;
}

- (UIButton *)jiaButton {
    if (!_jiaButton) {
        _jiaButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _jiaButton.layer.borderColor = [UIColor lightGrayColor].CGColor;
        _jiaButton.layer.borderWidth = .5f;
        [_jiaButton setTitle:@"+" forState:UIControlStateNormal];
        [_jiaButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_jiaButton addTarget:self action:@selector(addClick:) forControlEvents:UIControlEventTouchUpInside];
        _jiaButton.titleLabel.font = [UIFont systemFontOfSize:13];
    }
    return _jiaButton;
}

- (UITextField *)numberTextField {
    if (!_numberTextField) {
        _numberTextField = [[UITextField alloc]init];
        _numberTextField.textColor = [UIColor blackColor];
        _numberTextField.textAlignment = NSTextAlignmentCenter;
        _numberTextField.font = [UIFont systemFontOfSize:13];
        _numberTextField.layer.borderColor = [UIColor lightGrayColor].CGColor;
        _numberTextField.layer.borderWidth = .5f;
        _numberTextField.enabled = NO;
    }
    return _numberTextField;
}

#pragma mark - 选择
- (void)choiceClick:(UIButton *)sender {
    sender.selected = !sender.selected;
    self.model.isSelected = sender.selected;
    if (self.reloadTableView) {
        self.reloadTableView(self.model);
    }
}

- (void)addClick:(UIButton *)sender {
    NSInteger num = [self.numberTextField.text integerValue];

   

    _model.count = [NSString stringWithFormat:@"%ld",num + 1];
    _numberTextField.text = [NSString stringWithFormat:@"%ld",num + 1];
    if (_model.isSelected) {
        if (self.reloadTableView) {
            self.reloadTableView(self.model);
        }
    }
}


- (void)delClick:(UIButton *)sender {
   
    NSInteger num = [self.numberTextField.text integerValue];


    if (num > 1) {
        _model.count = [NSString stringWithFormat:@"%ld",num - 1];
        _numberTextField.text = [NSString stringWithFormat:@"%ld",num - 1];
        if (_model.isSelected) {
            if (self.reloadTableView) {
                self.reloadTableView(self.model);
            }
        }
    }
   
    
}
@end
